<?php
namespace Api\Service;


use AllowDynamicProperties;
use GuzzleHttp\Client;
use Illuminate\Support\Carbon;

#[AllowDynamicProperties] class YunyangService{

    public function __construct()
    {
        $this->domain = 'https://api.yunyangwl.com/api/sandbox/openService';
        $this->appid = env('KUAIDIAPPID');
        $this->secret = env('KUAIDIAPPKEY');
    }

    public function checkChannel($data)
    {
        $domain = "https://api.yunyangwl.com/api/wuliu/openService";
        $time = (string)Carbon::now()->valueOf();
        $requestId = md5(uniqid());
        $str = $this->appid.$requestId.$time.$this->secret;
        $sign = md5($str);
        $order = [];
        $order['serviceCode'] = "CHECK_CHANNEL_INTELLECT";
        $order['timeStamp'] = $time;
        $order['requestId'] = $requestId;
        $order['appid'] = $this->appid;
        $order['sign'] = $sign;
        $order['content'] = $data;
        $client=new Client(['timeout' => 5, 'verify' => false]);
//设置headers头
        $headers = ['Content-Type' => "application/json"];
        $response = $client->post($domain, [
            'headers' => $headers,
            'body' => json_encode($order,JSON_UNESCAPED_UNICODE)
        ]);
        return $response->getBody()->getContents();
    }

    public function createOrder($data)
    {
        $domain = "https://api.yunyangwl.com/api/wuliu/openService";
        $time = (string)Carbon::now()->valueOf();
        $requestId = md5(uniqid());
        $str = $this->appid.$requestId.$time.$this->secret;
        $sign = md5($str);
        $order = [];
        $order['serviceCode'] = "ADD_BILL_INTELLECT";
        $order['timeStamp'] = $time;
        $order['requestId'] = $requestId;
        $order['appid'] = $this->appid;
        $order['sign'] = $sign;
        $order['content'] = $data;
        $client=new Client(['timeout' => 5, 'verify' => false]);
//设置headers头
        $headers = ['Content-Type' => "application/json"];
        $response = $client->post($domain, [
            'headers' => $headers,
            'body' => json_encode($order,JSON_UNESCAPED_UNICODE)
        ]);
        return $response->getBody()->getContents();
    }

    public function getRoute(array $data)
    {
        $domain = "https://api.yunyangwl.com/api/wuliu/openService";
        $time = (string)Carbon::now()->valueOf();
        $requestId = md5(uniqid());
        $str = $this->appid.$requestId.$time.$this->secret;
        $sign = md5($str);
        $order = [];
        $order['serviceCode'] = "QUERY_TRANCE";
        $order['timeStamp'] = $time;
        $order['requestId'] = $requestId;
        $order['appid'] = $this->appid;
        $order['sign'] = $sign;
        $order['content'] = $data;
        $client=new Client(['timeout' => 5, 'verify' => false]);
//设置headers头
        $headers = ['Content-Type' => "application/json"];
        $response = $client->post($domain, [
            'headers' => $headers,
            'body' => json_encode($order,JSON_UNESCAPED_UNICODE)
        ]);
        return $response->getBody()->getContents();
    }

    public function cancelOrder(array $data)
    {
        $domain = "https://api.yunyangwl.com/api/wuliu/openService";
        $time = (string)Carbon::now()->valueOf();
        $requestId = md5(uniqid());
        $str = $this->appid.$requestId.$time.$this->secret;
        $sign = md5($str);
        $order = [];
        $order['serviceCode'] = "CANCEL";
        $order['timeStamp'] = $time;
        $order['requestId'] = $requestId;
        $order['appid'] = $this->appid;
        $order['sign'] = $sign;
        $order['content'] = $data;
        $client=new Client(['timeout' => 5, 'verify' => false]);
//设置headers头
        $headers = ['Content-Type' => "application/json"];
        $response = $client->post($domain, [
            'headers' => $headers,
            'body' => json_encode($order,JSON_UNESCAPED_UNICODE)
        ]);
        return $response->getBody()->getContents();
    }

    /**
     * @return string
     * @throws \GuzzleHttp\Exception\GuzzleException
     * 获取所有渠道信息
     */
    public function getAllChannel()
    {
        $domain = "https://api.yunyangwl.com/api/wuliu/openService";
        $time = (string)Carbon::now()->valueOf();
        $requestId = md5(uniqid());
        $str = $this->appid.$requestId.$time.$this->secret;
        $sign = md5($str);
        $order['serviceCode'] = "QUERY_ALL_CHANNEL";
        $order['timeStamp'] = $time;
        $order['requestId'] = $requestId;
        $order['appid'] = $this->appid;
        $order['sign'] = $sign;
        $order['content'] = new \stdClass();
        $client=new Client(['timeout' => 5, 'verify' => false]);
//设置headers头
        $headers = ['Content-Type' => "application/json"];
        $response = $client->post($domain, [
            'headers' => $headers,
            'body' => json_encode($order,JSON_UNESCAPED_UNICODE)
        ]);
        return $response->getBody()->getContents();

    }

    /**
     * @return string
     * @throws \GuzzleHttp\Exception\GuzzleException
     * 获取用户余额
     */
    public function getBalance()
    {
        $domain = "https://api.yunyangwl.com/api/wuliu/openService";
        $time = (string)Carbon::now()->valueOf();
        $requestId = md5(uniqid());
        $str = $this->appid.$requestId.$time.$this->secret;
        $sign = md5($str);
        $order['serviceCode'] = "QUERY_BALANCE";
        $order['timeStamp'] = $time;
        $order['requestId'] = $requestId;
        $order['appid'] = $this->appid;
        $order['sign'] = $sign;
        $order['content'] = new \stdClass();
        $client=new Client(['timeout' => 5, 'verify' => false]);
//设置headers头
        $headers = ['Content-Type' => "application/json"];
        $response = $client->post($domain, [
            'headers' => $headers,
            'body' => json_encode($order,JSON_UNESCAPED_UNICODE)
        ]);
        return $response->getBody()->getContents();
    }



}
